﻿

using System;
using System.Collections.Generic;
using ZFine.Code;
using ZFine.Data;
using ZFine.Domain.Entity;
using ZFine.Domain.IRepository.Project;
using ZFine.Domain.IRepository.SystemManage;
using ZFine.Repository.SystemManage;
using System.Linq;

namespace ZFine.Repository.Project
{
    public class InformMessagesRepository : RepositoryBase<InformMessagesEntity>, IInformMessagesRepository
    {
        private IUserRepository ServiceUser = new UserRepository();
        public void DeleteForm(string keyValue)
        {
            using (var db = new RepositoryBase().BeginTrans())
            {
                db.Delete<InformMessagesEntity>(t => t.F_Id == keyValue);
                db.Delete<SendCommercialEntity>(t => t.F_Id == keyValue);
                db.Commit();
            }
        }

        // 商户-群发
        public void SubmitFormSendC(InformMessagesEntity roleEntity)
        {
            using (var db = new RepositoryBase().BeginTrans())
            {
                List<SendCommercialEntity> roleAuthorizeEntitys = new List<SendCommercialEntity>();

                if (roleEntity != null)
                {
                    var LoginInfo = OperatorProvider.Provider.GetCurrent();
                    if (LoginInfo != null)
                    {
                        List<UserEntity> userlist = ServiceUser.IQueryable(f => f.F_IsAdministrator == false).ToList();
                        if (userlist.Count > 0)
                        {

                            for (int i = 0; i < userlist.Count; i++)
                            {
                                var obj = new SendCommercialEntity();
                                obj.Create();
                                obj.SendOfficeID = roleEntity.F_Id;// 获取消息
                                //  obj.Title = roleEntity.Title;
                                if (roleEntity.SendTime == null)
                                {
                                    obj.SendTime = DateTime.Now;
                                }
                                else
                                {
                                    obj.SendTime = roleEntity.SendTime;
                                }
                                //  obj.Content = roleEntity.Remark;
                                obj.SendUserName = userlist[i].F_Id;// 获取其他用户
                                obj.FromUserName = LoginInfo.UserName;// 获取发送者
                                obj.Type = roleEntity.Type;
                                obj.State = true;
                                obj.ReadState = 0;
                                roleAuthorizeEntitys.Add(obj);
                            }
                            //roleEntity.State = true;
                            db.Insert(roleAuthorizeEntitys);
                            db.Insert(roleEntity);
                        }
                    }
                }

                else
                {
                    roleEntity.State = false;
                    db.Insert(roleEntity);
                }
                //db.Delete<SendCommercialEntity>(t => t.SendOfficeID == roleEntity.F_Id);
                //db.Insert(roleAuthorizeEntitys);
                db.Commit(); //已包含事务回滚
            }
        }
        //会员-群发
        public void SubmitFormSendM(InformMessagesEntity roleEntity)
        {
            using (var db = new RepositoryBase().BeginTrans())
            {
                List<SendMembersInfosEntity> roleAuthorizeEntitys = new List<SendMembersInfosEntity>();

                if (roleEntity != null)
                {
                    var LoginInfo = OperatorProvider.Provider.GetCurrent();
                    if (LoginInfo != null)
                    {
                        List<UserEntity> userlist = ServiceUser.IQueryable(f => f.F_IsAdministrator == false).ToList();
                        if (userlist.Count > 0)
                        {

                            for (int i = 0; i < userlist.Count; i++)
                            {
                                var obj = new SendMembersInfosEntity();
                                obj.Create();
                                obj.SendOfficeID = roleEntity.F_Id;// 获取消息
                                //  obj.Title = roleEntity.Title;
                                if (roleEntity.SendTime == null)
                                {
                                    obj.SendTime = DateTime.Now;
                                }
                                else
                                {
                                    obj.SendTime = roleEntity.SendTime;
                                }
                                //  obj.Content = roleEntity.Remark;
                                obj.SendUserName = userlist[i].F_Id;// 获取其他用户
                                obj.FromUserName = LoginInfo.UserName;// 获取发送者
                                obj.Type = roleEntity.Type;
                                obj.State = true;
                                obj.ReadState = 0;
                                roleAuthorizeEntitys.Add(obj);
                            }
                            //roleEntity.State = true;
                            db.Insert(roleAuthorizeEntitys);
                            db.Insert(roleEntity);
                        }
                    }
                }

                else
                {
                    roleEntity.State = false;
                    db.Insert(roleEntity);
                }
                //db.Delete<SendCommercialEntity>(t => t.SendOfficeID == roleEntity.F_Id);
                //db.Insert(roleAuthorizeEntitys);
                db.Commit(); //已包含事务回滚
            }
        }
    }
}
